<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>javaEE(SSM)学习笔记3：Mybatis核心对象及其配置 | Space</title><meta name="keywords" content="笔记 学习"><meta name="author" content="Heldwen"><meta name="copyright" content="Heldwen"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><meta name="description" content="总体思路： SqlSession是实际执行SQL的接口，其从Mybatis的核心对象SqlSessionFactory接口的方法openSession获取；而SqlSessionFactory接口的实现类由SqlSessionFactoryBuilder对象创建。  1.核心对象(了解) SqlSessionFactory接口对象是Mybatis的核心对象，由SqlSessionFactoryBu">
<meta property="og:type" content="article">
<meta property="og:title" content="javaEE(SSM)学习笔记3：Mybatis核心对象及其配置">
<meta property="og:url" content="https://gitee.com/drh-space/myblog/03/04/2022/javaEE(SSM)%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B03%EF%BC%9AMybatis%E6%A0%B8%E5%BF%83%E5%AF%B9%E8%B1%A1%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/index.html">
<meta property="og:site_name" content="Space">
<meta property="og:description" content="总体思路： SqlSession是实际执行SQL的接口，其从Mybatis的核心对象SqlSessionFactory接口的方法openSession获取；而SqlSessionFactory接口的实现类由SqlSessionFactoryBuilder对象创建。  1.核心对象(了解) SqlSessionFactory接口对象是Mybatis的核心对象，由SqlSessionFactoryBu">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg">
<meta property="article:published_time" content="2022-03-04T15:23:48.016Z">
<meta property="article:modified_time" content="2022-03-10T04:49:49.918Z">
<meta property="article:author" content="Heldwen">
<meta property="article:tag" content="笔记 学习">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg"><link rel="shortcut icon" href="/myblog/img/favicon.png"><link rel="canonical" href="https://gitee.com/drh-space/myblog/03/04/2022/javaEE(SSM)%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B03%EF%BC%9AMybatis%E6%A0%B8%E5%BF%83%E5%AF%B9%E8%B1%A1%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="yandex-verification" content="{&quot;theme_color&quot;:{&quot;enable&quot;:true,&quot;main&quot;:&quot;#49B1F5&quot;,&quot;paginator&quot;:&quot;#00c4b6&quot;,&quot;button_hover&quot;:&quot;#FF7242&quot;,&quot;text_selection&quot;:&quot;#00c4b6&quot;,&quot;link_color&quot;:&quot;#99a9bf&quot;,&quot;meta_color&quot;:&quot;#858585&quot;,&quot;hr_color&quot;:&quot;#A4D8FA&quot;,&quot;code_foreground&quot;:&quot;#F47466&quot;,&quot;code_background&quot;:&quot;rgba(27, 31, 35, .05)&quot;,&quot;toc_color&quot;:&quot;#00c4b6&quot;,&quot;blockquote_padding_color&quot;:&quot;#49b1f5&quot;,&quot;blockquote_background_color&quot;:&quot;#49b1f5&quot;}}"/><link rel="stylesheet" href="/myblog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css"><script>var GLOBAL_CONFIG = { 
  root: '/myblog/',
  hexoversion: '5.2.0',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  ClickShowText: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  justifiedGallery: {
    js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
    css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
  },
  isPhotoFigcaption: true,
  islazyload: true,
  isanchor: false
};

var saveToLocal = {
  set: function setWithExpiry(key, value, ttl) {
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isSidebar: true,
  postUpdate: '03-10-2022 12:49:49'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {
  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }

  const autoChangeMode = 'true'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified

    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }
})()</script><meta name="generator" content="Hexo 5.2.0"><link rel="alternate" href="/myblog/atom.xml" title="Space" type="application/atom+xml">
</head><body><div id="mobile-sidebar"><div id="menu_mask"></div><div id="mobile-sidebar-menus"><div class="mobile_author_icon"><img class="avatar-img" data-lazy-src="/myblog/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="mobile_post_data"><div class="mobile_data_item is-center"><div class="mobile_data_link"><a href="/myblog/archives/"><div class="headline">文章</div><div class="length_num">7</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/myblog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/%E5%8A%A8%E6%80%81/"><i class="fa-fw fas fa-archive"></i><span> 动态</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/%E6%A0%87%E7%AD%BE/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/%E5%88%86%E7%B1%BB/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/myblog/%E9%9F%B3%E4%B9%90/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page" href="/myblog/%E7%94%B5%E5%BD%B1/"><i class="fa-fw fas fa-movies"></i><span> 电影</span></a></li><li><a class="site-page" href="/myblog/%E6%97%85%E8%A1%8C/"><i class="fa-fw fas fa-travel"></i><span> 旅行</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/myblog/%E5%85%B3%E4%BA%8E/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div id="body-wrap"><div id="web_bg"></div><div id="sidebar"><i class="fas fa-arrow-right on" id="toggle-sidebar"></i><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar">     </div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%BB%E4%BD%93%E6%80%9D%E8%B7%AF%EF%BC%9A"><span class="toc-number">1.</span> <span class="toc-text">总体思路：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%A0%B8%E5%BF%83%E5%AF%B9%E8%B1%A1-%E4%BA%86%E8%A7%A3"><span class="toc-number"></span> <span class="toc-text">1.核心对象(了解)</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-SqlSessionFactoryBuilder"><span class="toc-number"></span> <span class="toc-text">1.1 SqlSessionFactoryBuilder</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-SqlSessionFactory"><span class="toc-number"></span> <span class="toc-text">1.2 SqlSessionFactory</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-3-SqlSession"><span class="toc-number"></span> <span class="toc-text">1.3.SqlSession</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-Mybatis%E7%9A%84%E9%85%8D%E7%BD%AE"><span class="toc-number"></span> <span class="toc-text">2.Mybatis的配置</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-Properties"><span class="toc-number"></span> <span class="toc-text">2.1 Properties</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-settings"><span class="toc-number"></span> <span class="toc-text">2.2 settings</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-typeAliases"><span class="toc-number"></span> <span class="toc-text">2.3 typeAliases</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-environments"><span class="toc-number"></span> <span class="toc-text">2.4 environments</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-5-mappers"><span class="toc-number"></span> <span class="toc-text">2.5 mappers</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6"><span class="toc-number"></span> <span class="toc-text">3.映射文件</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1%E6%95%B4%E4%BD%93%E7%BB%93%E6%9E%84"><span class="toc-number"></span> <span class="toc-text">3.1整体结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-SQL%E6%98%A0%E5%B0%84%E8%AF%AD%E5%8F%A5"><span class="toc-number"></span> <span class="toc-text">3.2 SQL映射语句</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-1-select-%E6%98%A0%E5%B0%84%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.</span> <span class="toc-text">3.2.1 select 映射语句</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E8%AF%AD%E6%B3%95%E6%A0%BC%E5%BC%8F%EF%BC%9A"><span class="toc-number">1.0.1.</span> <span class="toc-text">1.语法格式：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-2-insert-%E6%98%A0%E5%B0%84%E8%AF%AD%E5%8F%A5"><span class="toc-number">2.</span> <span class="toc-text">3.2.2 insert 映射语句</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E8%AF%AD%E6%B3%95%E6%A0%BC%E5%BC%8F%EF%BC%9A-1"><span class="toc-number">2.0.1.</span> <span class="toc-text">1.语法格式：</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-%E7%A4%BA%E4%BE%8B%EF%BC%9A"><span class="toc-number">2.0.2.</span> <span class="toc-text">2.示例：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-3-Upadet%E5%92%8Cdelete%E6%98%A0%E5%B0%84%E8%AF%AD%E5%8F%A5"><span class="toc-number">3.</span> <span class="toc-text">3.2.3 Upadet和delete映射语句</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E4%BD%BF%E7%94%A8log4j%E6%9F%A5%E7%9C%8BSQL%E6%89%A7%E8%A1%8C%E7%9A%84%E8%BE%93%E5%87%BA"><span class="toc-number"></span> <span class="toc-text">4.使用log4j查看SQL执行的输出</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E5%BC%95%E5%85%A5%E4%BE%9D%E8%B5%96"><span class="toc-number">0.1.</span> <span class="toc-text">1.引入依赖</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E6%B7%BB%E5%8A%A0log4j-properties%E6%96%87%E4%BB%B6"><span class="toc-number">0.2.</span> <span class="toc-text">2.添加log4j.properties文件</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E4%BD%BF%E7%94%A8Mapper%E6%8E%A5%E5%8F%A3%EF%BC%88%E5%AE%9E%E9%99%85%E5%BA%94%E7%94%A8%EF%BC%89"><span class="toc-number"></span> <span class="toc-text">5.使用Mapper接口（实际应用）</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-%E5%85%B6%E4%BB%96%EF%BC%9A%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%92%8C%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6%E6%A0%87%E8%AE%B0%E8%87%AA%E5%8A%A8%E6%8F%90%E7%A4%BA%E5%8A%9F%E8%83%BD"><span class="toc-number"></span> <span class="toc-text">6.其他：实现配置文件和映射文件标记自动提示功能</span></a></div></div></div><header class="post-bg" id="page-header" style="background-image: url(https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg)"><nav id="nav"><span id="blog_name"><a id="site-name" href="/myblog/">Space</a></span><span id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/myblog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/%E5%8A%A8%E6%80%81/"><i class="fa-fw fas fa-archive"></i><span> 动态</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/%E6%A0%87%E7%AD%BE/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/%E5%88%86%E7%B1%BB/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/myblog/%E9%9F%B3%E4%B9%90/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page" href="/myblog/%E7%94%B5%E5%BD%B1/"><i class="fa-fw fas fa-movies"></i><span> 电影</span></a></li><li><a class="site-page" href="/myblog/%E6%97%85%E8%A1%8C/"><i class="fa-fw fas fa-travel"></i><span> 旅行</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/myblog/%E5%85%B3%E4%BA%8E/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><span class="close" id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></span></span></nav><div id="post-info"><div id="post-title"><div class="posttitle">javaEE(SSM)学习笔记3：Mybatis核心对象及其配置</div></div><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-03-04T15:23:48.016Z" title="发表于 03-04-2022 23:23:48">03-04-2022</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-03-10T04:49:49.918Z" title="更新于 03-10-2022 12:49:49">03-10-2022</time></span></div><div class="meta-secondline"> <span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout_post" id="content-inner"><article id="post"><div class="post-content" id="article-container"><h3 id="总体思路："><a href="#总体思路：" class="headerlink" title="总体思路："></a>总体思路：</h3><blockquote>
<p>SqlSession是实际执行SQL的接口，其从Mybatis的核心对象SqlSessionFactory接口的方法openSession获取；而SqlSessionFactory接口的实现类由SqlSessionFactoryBuilder对象创建。</p>
</blockquote>
<h1 id="1-核心对象-了解"><a href="#1-核心对象-了解" class="headerlink" title="1.核心对象(了解)"></a>1.核心对象(了解)</h1><blockquote>
<p>SqlSessionFactory接口对象是Mybatis的核心对象，由SqlSessionFactoryBuilder的方法创建。</p>
</blockquote>
<h2 id="1-1-SqlSessionFactoryBuilder"><a href="#1-1-SqlSessionFactoryBuilder" class="headerlink" title="1.1 SqlSessionFactoryBuilder"></a>1.1 SqlSessionFactoryBuilder</h2><p>（1）主要作用</p>
<p>使用build方法，读取并解析Mybatis的XML配置文件，将解析结果保存到配置对象，并创建和返回SqlSessionFactory实现类（DefaultSqlSessionFactory）</p>
<blockquote>
<p>1.解析属性，或读取属性文件(properties);</p>
<p>2.映射POJO类的别名;(typeAliases)</p>
<p>3.读取mybatis的全局配置(settings)</p>
<p>4.构建数据源和事务管理器(environments)等。</p>
<p>5.装载mapper文件，将不同命名空间的mapper文件中的id对应sql语句分离出来，使用Map类型对象保存为键值对的形式。（mappers）。</p>
<p>这些将保存在配置对象Configuration中。</p>
<p>6.返回mybatis的核心对象defaultSqlSessionfactory</p>
</blockquote>
<p>（2）builder方法最常用的格式（建议使用）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">builder(Reader)</span><br></pre></td></tr></table></figure>

<p>如果在配置文件定义了不同的环境(environment)或属性文件(properties)，那么可以使用以下这个方法（不建议）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">build(reader,&quot;environment id&quot;,properties属性对象)</span><br><span class="line">其中参数3属性对象使用：</span><br><span class="line">	Properties p&#x3D;new Properties();&#x2F;&#x2F;java.util.Properties;</span><br><span class="line">	p.load(Reader);	或逐个赋值	p.put(&quot;属性名&quot;,&quot;属性值&quot;);</span><br></pre></td></tr></table></figure>

<h2 id="1-2-SqlSessionFactory"><a href="#1-2-SqlSessionFactory" class="headerlink" title="1.2 SqlSessionFactory"></a>1.2 SqlSessionFactory</h2><p>接口（会话），保存buildr方法返回的DefaultSqlSessionFactory实现类对象。</p>
<p>主要作用：在调用openSession方法时，初始化数据库的执行环境。</p>
<blockquote>
<p>1.是否开启事务(默认开启，即不自动提交)</p>
<p>2.事务的隔离级别（默认，知道即可）</p>
<p>3.是否不采用配置文件的连接（不建议使用，默认采用配置文件中数据源的连接connection）</p>
<p>4.设置执行器类型：1.SIMPLE;2.REUSE;3.BATCH  (默认SIMPLE ，即为每一条要执行的SQL创建预处理语句preparedStatement；REUSE，缓存预处理语句；BATCH用于批处理，当批量插入和更新数据时使用)</p>
</blockquote>
<h2 id="1-3-SqlSession"><a href="#1-3-SqlSession" class="headerlink" title="1.3.SqlSession"></a>1.3.SqlSession</h2><p>接口（会话），保存由openSession方法返回的DefaultSqlSession实现类对象。</p>
<p>主要作用：根据方法参数，调用其内部的执行器Executor来执行SQL语句。真正执行SQL的对象。</p>
<blockquote>
<p>1.从配置对象中查询要执行的id及其对应的sql语句。</p>
<p>2.分析输入参数，创建并填充到预处理语句中;</p>
<p>3.使用执行器执行sql语句</p>
<p>4.将处理结果映射到java对象</p>
</blockquote>
<p><strong>只需要记住SqlSession的主要方法</strong></p>
<blockquote>
<p>1.返回一条语句；<br>2.返回一组语句；<br>3.插入数据；<br>4.更新数据;<br>5.提交更新<br>6.关闭</p>
</blockquote>
<p><strong>注意：上面的内容了解即可，不必深入，因为只是在学习mybatis框架中会用到，而最后在Web应用整合SSM框架中，我们并不会直接使用到。</strong></p>
<blockquote>
<p>主要的内容：还是mapper文件的sql的编写，以及极少部分在mybatis的配置文件中的内容。</p>
</blockquote>
<h1 id="2-Mybatis的配置"><a href="#2-Mybatis的配置" class="headerlink" title="2.Mybatis的配置"></a>2.Mybatis的配置</h1><blockquote>
<p>注意配置顺序！需要依次按照以下顺序配置（假如需要使用）</p>
</blockquote>
<h2 id="2-1-Properties"><a href="#2-1-Properties" class="headerlink" title="2.1 Properties"></a>2.1 Properties</h2><p>作用：配置一组属性，在其后面可通过${属性名}引用,<strong>类似定义一组变量</strong>。一般用于定义数据源各个属性值。</p>
<p>基本用法：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">properties</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;属性名&quot;</span> <span class="attr">value</span>=<span class="string">&quot;属性值&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>通常用法：将配置内容保存在一个后缀名为properties的文件中，在因为该文件时，使用以下语法引入xml文件：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">properties</span> <span class="attr">resource</span>=<span class="string">&quot;文件位置(类路径)&quot;</span> /&gt;</span></span><br><span class="line">如：</span><br><span class="line"><span class="tag">&lt;<span class="name">properties</span> <span class="attr">resource</span>=<span class="string">&quot;db.properties&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure>

<p>db.properties文件格式（通常放在src/main/resource目录中）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#这是属性文件的注释符</span><br><span class="line">driver&#x3D;com.mysql.cj.jdbc.Driver</span><br><span class="line">url&#x3D;jdbc:mysql:&#x2F;&#x2F;localhost:3306&#x2F;mybatis?serverTimezone&#x3D;Asia&#x2F;Shanghai</span><br><span class="line">uname&#x3D;root</span><br><span class="line">pwd&#x3D;root</span><br></pre></td></tr></table></figure>

<blockquote>
<p>src/main/resource中任何根目录和子目录的内容都将编译到类路径根目录下，因此resource属性值不必带子文件夹名。</p>
</blockquote>
<h2 id="2-2-settings"><a href="#2-2-settings" class="headerlink" title="2.2 settings"></a>2.2 settings</h2><p>作用：mybatis全局配置开关，会改变 MyBatis 的运行时的行为。</p>
<p>基本语法：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">settings</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;属性名&quot;</span> <span class="attr">value</span>=<span class="string">&quot;属性值&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">settings</span>&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>cacheEnabled ：开启二级缓存的全局开关，默认为true</p>
<p>lazyLoadingEnabled：延迟加载的全局开关。 默认值为false，在关联映射的嵌套查询中使用才有效。</p>
<p>mapUnderscoreToCamelCase：默认false,是否开启自动驼峰命名规则（camel case）映射，即从经典数据库列名 user_name 到经典 Java 属性名 userName 的类似映射。[如果数据表中字段名字带下划线，那么可以自动对应到对象的Camel命名的属性中]</p>
</blockquote>
<p>其他很少使用，后者学完Mybatis回头理解。</p>
<h2 id="2-3-typeAliases"><a href="#2-3-typeAliases" class="headerlink" title="2.3 typeAliases"></a>2.3 typeAliases</h2><p>默认的情况下，在mapper文件中，输入参数和输出参数如果使用到POJO，那么需要使用全限定名，即包名+类名的方式，写起来不方面，那么可以使用该标签给POJO一个简短的别名来使用。</p>
<p>通常用法：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">typeAliases</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">&quot;POJO中的包名&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">typeAliases</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>这样，所有用到POJO包中的类，在mapper文件中，都可以直接使用类名，并且不区分大小写。例如：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;getCustomer&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;pojo.Customer&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;pojo.Customer&quot;</span>&gt;</span></span><br><span class="line">		 ...</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span> </span><br></pre></td></tr></table></figure>

<p>可以写为：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;getCustomer&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;customer&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;customer&quot;</span>&gt;</span></span><br><span class="line">	...</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span> </span><br></pre></td></tr></table></figure>
<h2 id="2-4-environments"><a href="#2-4-environments" class="headerlink" title="2.4 environments"></a>2.4 environments</h2><p>作用：配置数据环境，数据源和事务管理器,见下面注释。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--配置内容：主要是mybatis的运行环境 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!--可以配置测试环境、生产环境，default指定当前环境environment的id --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">&quot;mysql&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">&lt;!--当前环境，id是自定义，唯一的，学习过程就这么一个 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">&quot;mysql&quot;</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!--1.必须配置的属性，学习过程就指定JDBC即可 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">&quot;JDBC&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">		<span class="comment">&lt;!--2.必须配置的属性，如何连接到数据库 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">&quot;POOLED&quot;</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;driver&quot;</span> <span class="attr">value</span>=<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span> /&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;url&quot;</span></span></span><br><span class="line"><span class="tag">				<span class="attr">value</span>=<span class="string">&quot;jdbc:mysql://localhost:3306/mybatis?</span></span></span><br><span class="line"><span class="tag"><span class="string">                                       serverTimezone=Asia/Shanghai&quot;</span> /&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span> <span class="attr">value</span>=<span class="string">&quot;root&quot;</span> /&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span> <span class="attr">value</span>=<span class="string">&quot;root&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">environment</span>&gt;</span> </span><br><span class="line">	</span><br><span class="line"><span class="tag">&lt;/<span class="name">environments</span>&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>除了连接数据库的属性需要熟悉；其他在学习过程中基本不变；</p>
<p><strong>该配置在SSM整合框架时不会使用到；仅在测试时使用。</strong></p>
</blockquote>
<h2 id="2-5-mappers"><a href="#2-5-mappers" class="headerlink" title="2.5 mappers"></a>2.5 mappers</h2><p>在每次创建好mapper文件(XML)时，需要在mybatis<strong>配置文件</strong>引入。</p>
<blockquote>
<p>注意：经常会忘记配置装载新创建的mapper文件</p>
</blockquote>
<p>语法格式：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mappers</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">&quot;类路径的mapper.xml文件&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mappers</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>通常，我们会将mapper.xml文件保存在src/main/java/mapper包(原因：后面需要与对应的java接口在一个位置)中，例如：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mappers</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!--映射文件，包名/MAPPER文件名.xml --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">&quot;mapper/CustomerMapper.xml&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mappers</span>&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>mapper属性的其他用法在学习到当前为止，还不可用。</p>
<p>类路径：把包当作文件夹使用</p>
</blockquote>
<h1 id="3-映射文件"><a href="#3-映射文件" class="headerlink" title="3.映射文件"></a>3.映射文件</h1><h2 id="3-1整体结构"><a href="#3-1整体结构" class="headerlink" title="3.1整体结构"></a>3.1整体结构</h2><p><strong>1.根元素：mapper</strong></p>
<blockquote>
<p>（1）包含namespace属性，主要区分在不同Mapper文件中，SQL映射标记可以有相同的id；类似一个同名的类名，可以包含在不同的包中。<br>（2）注意：后面使用有约束条件，也就是当前命名空间名采用与Mapper.xml文件同名，即：包名.Mapper文件名，<strong>因为后面会采用接口对应映射文件，接口名必须与Mapper文件同名，实际上namespace就是就这个接口的全限定名(包名+接口名，因为接口名必须与Mapper文件同名，所以你也可以认为是Mapper.xml的文件名)。</strong></p>
</blockquote>
<p><strong>2.缓存cache</strong></p>
<p>表示是否开启二级缓存（保存<strong>查询</strong>结果在缓存，再次读取不会再到数据库重复查询），该标记标识所有该mapper的查询都开启二级缓存，</p>
<blockquote>
<p>（1）二级缓存是不同sqlSession对象共享的，要开启二级缓存，mybatis配置文件中settings中的选项cacheEanabled必须为true(默认就是true).</p>
<p>（2）一级缓存是sqlSession对象本身独有的，只要不关闭，那么重复执行同一个查询都是从缓存获取结果，跟全局设置和mapper文件中的cahe设置无关.</p>
<p><strong>ps</strong>：<br>a.当在select语句中使用了 flushCache=true后，一二级缓存都无效，每次都会重新查询数据库；</p>
<p>b.如果使用了useCache=false，那么二级缓存无效。</p>
<p><strong>结论：如果要开启二级缓存：<br>1.必须让全局配置中的cacheEnabled=true;<br>2.mapper文件根节点下使用<cache />标记；<br>3.select标记属性 useCache=true,flushCache=false (这两者都默认，不使用就是了)。</strong></p>
</blockquote>
<p><strong>实际上就是，什么都可以不设置，除了在mapper文件根节点下使用<cache/>就可以了。</strong></p>
<p><strong>3.SQL语句标记</strong></p>
<p>包含select / insert /update /delete标记</p>
<p> <strong>4.sql标记</strong></p>
<p>相当定义了公共的SQL部分重复的语句，在文件的任何语句中可以引用，但是这样阅读性很不好，不是非不得已不使用。</p>
<p><strong>5.resultMap</strong></p>
<p><strong>很重要的一个属性</strong><br>作用：</p>
<blockquote>
<p>1）.在表字段和POJO属性名不对应情况下，手动将查询结果赋值。<br>2）.在关联映射中使用，给POJO复合属性赋值。</p>
</blockquote>
<p>在后面（关联查询）使用也有到。</p>
<h2 id="3-2-SQL映射语句"><a href="#3-2-SQL映射语句" class="headerlink" title="3.2 SQL映射语句"></a>3.2 SQL映射语句</h2><h3 id="3-2-1-select-映射语句"><a href="#3-2-1-select-映射语句" class="headerlink" title="3.2.1 select 映射语句"></a>3.2.1 select 映射语句</h3><h5 id="1-语法格式："><a href="#1-语法格式：" class="headerlink" title="1.语法格式："></a>1.语法格式：</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;类似方法名&quot;</span> <span class="attr">paramerType</span>=<span class="string">&quot;输入参数类型&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;返回结果类型&quot;</span> 其他属性 &gt;</span></span><br><span class="line">	select * from 表名 where 条件 </span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>示例1：一般查询</strong></p>
<p>需求：根据id查询user表的信息。</p>
<p>分析：传入参数id，普通整数类型；输出参数POJO—User.java</p>
<p>约定：所有的Mapper文件放在mapper包中；所有的POJO类放在pojo包中。</p>
<p>实现：</p>
<blockquote>
<p>1.创建数据库mybatis并添加表user</p>
<p>2.创建项目，添加必要依赖: mybatis / connectorJ</p>
<p>3.添加mybatis的配置文件；</p>
<p>4.添加mapper包和UserMapper.xml文件；</p>
<p>5.添加pojo包和User.java实体类。</p>
</blockquote>
<p>—创建步骤参见javaEE(SSM)学习笔记2：MyBatis入门章节。</p>
<p>pojo—User.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line">	<span class="keyword">int</span> id;</span><br><span class="line">	String username;</span><br><span class="line">    <span class="comment">//省略GETTER/SETTER生成的方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>数据库的表结构与pojo一致，自行创建和输入数据</p>
</blockquote>
<p>UserMapper.xml的实现</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">&quot;mapper.UserMapper&quot;</span>&gt;</span> </span><br><span class="line">	<span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;getUserById&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;int&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;user&quot;</span>  &gt;</span></span><br><span class="line">		select * from user where id=#&#123;id&#125;	</span><br><span class="line">	<span class="tag">&lt;/<span class="name">select</span>&gt;</span> </span><br><span class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>注意：配置文件中使用了<typeAliases />标记，所以这里可以使用别名user</p>
<p>对普通类型参数,实际语句中占位符可以使用下标0</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select * from user where id&#x3D;#&#123;0&#125;	</span><br></pre></td></tr></table></figure>
</blockquote>
<p>执行：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//省略了其他代码</span></span><br><span class="line">User user=sqlSession.selectOne(<span class="string">&quot;getUserById&quot;</span>,<span class="number">1</span>); </span><br></pre></td></tr></table></figure>



<p><strong>示例2：模糊查询</strong></p>
<p>如果要获取一组数据，比如根据用户名进行模糊查询：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">&quot;mapper.UserMapper&quot;</span>&gt;</span> </span><br><span class="line">	<span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;getUserByName&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;string&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;user&quot;</span>  &gt;</span></span><br><span class="line">		select * from user where  username like concat(&#x27;%&#x27;,#&#123;uname&#125;,&#x27;%&#x27;)	</span><br><span class="line">	<span class="tag">&lt;/<span class="name">select</span>&gt;</span> </span><br><span class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>输出类型仍然是user，虽然查询结果是List<User> 但输出类型是指每一个元素的类型</p>
<p>concat用于字符串连接；uname仅仅是占位符，任意的，也可以使用0下标</p>
</blockquote>
<p>执行:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">List&lt;User&gt; users=sqlSession3.selectList(<span class="string">&quot;getUserByName&quot;</span>,<span class="string">&quot;&quot;</span>);</span><br></pre></td></tr></table></figure>

<blockquote>
<p>结果返回一条记录，使用selectOne；返回一组记录：使用selectList</p>
</blockquote>
<p> <strong>示例3：resultMpa使用</strong></p>
<p>当实体类属性与所查询表字段名不一致，可以借助resultMap属性和resultMap标记（两者联合使用，使用id对应），将查询结果手工给实体类属性赋值。</p>
<p>需求：根据id查询用户信息</p>
<p>1.假定当前POJO类为User2.java，与上例的User.java相比，是属性名不同。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User2</span> </span>&#123;</span><br><span class="line">	<span class="keyword">int</span> uid;</span><br><span class="line">	String uname;</span><br><span class="line">    <span class="comment">//省略GETTER/SETTER</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>2.Select映射语句</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id=<span class="string">&quot;getById&quot;</span> parameterType=<span class="string">&quot;int&quot;</span> resultMap=<span class="string">&quot;userMap&quot;</span>&gt;</span><br><span class="line">	select * from user where id=#&#123;id&#125;</span><br><span class="line">&lt;/select&gt;</span><br><span class="line"></span><br><span class="line">&lt;resultMap id=<span class="string">&quot;userMap&quot;</span> type=<span class="string">&quot;user2&quot;</span>&gt;</span><br><span class="line">	&lt;id property=<span class="string">&quot;uid&quot;</span> column=<span class="string">&quot;id&quot;</span>/&gt;</span><br><span class="line">	&lt;result property=<span class="string">&quot;uname&quot;</span> column=<span class="string">&quot;username&quot;</span> /&gt;</span><br><span class="line">&lt;/resultMap&gt;</span><br></pre></td></tr></table></figure>



<blockquote>
<p>userMap是resultMap标记的id；每个resultMap必须有id属性，以及type属性，type属性含义是查询结果的要映射的类型。（代表你自己手工将字段值给该类型属性赋值）</p>
<p>id标记是是指查询的该列是主键。你一样可以使用result标记代替。</p>
<p>result标记中的property 特指类的哪个属性；column特指表的字段名（列名），如果查询语句使用了别名，那么对应的是别名。</p>
</blockquote>
<h3 id="3-2-2-insert-映射语句"><a href="#3-2-2-insert-映射语句" class="headerlink" title="3.2.2 insert 映射语句"></a>3.2.2 insert 映射语句</h3><p>作用：插入数据到表</p>
<h5 id="1-语法格式：-1"><a href="#1-语法格式：-1" class="headerlink" title="1.语法格式："></a>1.语法格式：</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">&quot;&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;&quot;</span> &gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>注意，insert和update/delete不需要设置返回值类型，都是整数类型</p>
<h5 id="2-示例："><a href="#2-示例：" class="headerlink" title="2.示例："></a>2.示例：</h5><p>需求：将用户数据插入到表中。</p>
<p>UserMapper.xml的实现</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">&quot;addUser&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;user&quot;</span>&gt;</span></span><br><span class="line">	insert into user(username) values(#&#123;username&#125;)</span><br><span class="line"><span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>传入的是POJO，占位符值为POJO的属性，不是任意的。</strong></p>
<p>执行：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">User u=<span class="keyword">new</span> User();</span><br><span class="line">u.setUsername(<span class="string">&quot;张三abc&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> c=sqlSession.insert(<span class="string">&quot;addUser&quot;</span>,u);</span><br><span class="line"><span class="comment">//默认开启了事务，需要手动提交</span></span><br><span class="line">sqlSession.commit();</span><br></pre></td></tr></table></figure>

<blockquote>
<p>注意：insert和update、delete都需要手动提交commit，才会真正保存到数据库。</p>
<p>如果自动提交（不使用事务），那么可以这样获取sqlSession对象：</p>
<p>SqlSession sqlSession=ssf.openSession(true);带参数true</p>
</blockquote>
<p>经常会有这么需求（通常是关联表）：插入数据时，获取当前记录的id</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;insert id=<span class="string">&quot;addUserGetId&quot;</span> parameterType=<span class="string">&quot;user&quot;</span> keyProperty=<span class="string">&quot;id&quot;</span></span><br><span class="line">	useGeneratedKeys=<span class="string">&quot;true&quot;</span>&gt;</span><br><span class="line">	<span class="function">insert into <span class="title">user</span><span class="params">(username)</span> <span class="title">values</span><span class="params">(#&#123;username&#125;)</span></span></span><br><span class="line"><span class="function">&lt;/insert&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>使用keyProperty=”POJO属性名” ，该属性保存插入数据后的id，</p>
<p>配合useGeneratedKeys=”true”使用，表示需要获取id</p>
</blockquote>
<p>执行：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">SqlSession sqlSession3=ssf.openSession(<span class="keyword">true</span>);</span><br><span class="line">User u=<span class="keyword">new</span> User();</span><br><span class="line">u.setUsername(<span class="string">&quot;张三abc&quot;</span>);</span><br><span class="line"><span class="keyword">int</span> c=sqlSession3.insert(<span class="string">&quot;addUserGetId&quot;</span>,u);</span><br><span class="line"></span><br><span class="line">System.out.println(<span class="string">&quot;id=&quot;</span>+u.getId()); <span class="comment">//id保存在u对象的属性id中	</span></span><br></pre></td></tr></table></figure>

<h3 id="3-2-3-Upadet和delete映射语句"><a href="#3-2-3-Upadet和delete映射语句" class="headerlink" title="3.2.3 Upadet和delete映射语句"></a>3.2.3 Upadet和delete映射语句</h3><p>实际上，理解上面的实现过程，这两个映射基本都与insert映射语句相类似的使用。可以借鉴使用。</p>
<h1 id="4-使用log4j查看SQL执行的输出"><a href="#4-使用log4j查看SQL执行的输出" class="headerlink" title="4.使用log4j查看SQL执行的输出"></a>4.使用log4j查看SQL执行的输出</h1><p>在调试时，非常方便查看SQL语句的执行情况，如输入参数、输出结果</p>
<h4 id="1-引入依赖"><a href="#1-引入依赖" class="headerlink" title="1.引入依赖"></a>1.引入依赖</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2.17<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="2-添加log4j-properties文件"><a href="#2-添加log4j-properties文件" class="headerlink" title="2.添加log4j.properties文件"></a>2.添加log4j.properties文件</h4><p>该文件固定文件名，使用以下给出的内容自行编辑；该文件需要自行创建并添加到src/main/java的根目录中</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Global logging configuration </span></span><br><span class="line"><span class="meta">log4j.rootLogger</span>=<span class="string">ERROR, stdout </span></span><br><span class="line"><span class="comment"># MyBatis logging configuration... </span></span><br><span class="line"><span class="meta">log4j.logger.mapper</span>=<span class="string">TRACE </span></span><br><span class="line"><span class="comment"># Console output... </span></span><br><span class="line"><span class="meta">log4j.appender.stdout</span>=<span class="string">org.apache.log4j.ConsoleAppender </span></span><br><span class="line"><span class="meta">log4j.appender.stdout.layout</span>=<span class="string">org.apache.log4j.PatternLayout </span></span><br><span class="line"><span class="meta">log4j.appender.stdout.layout.ConversionPattern</span>=<span class="string">%5p [%t] - %m%n</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>只要修改第4行，格式如下：</p>
<p>log4j.logger.<strong>Mapper文件所在的包名</strong>=TRACE</p>
<p>log4j.logger.你的mapper包名；特别注意：你的mapperXML文件的namespace也必须是这个包，否则可能没有输出调试信息。</p>
</blockquote>
<p>在JUint测试时，会在控制台自动输出Mapper.XML文件中所有执行的SQL语句过程，如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">DEBUG [main] - &#x3D;&#x3D;&gt;  Preparing: insert into user(username) values(?)</span><br><span class="line">DEBUG [main] - &#x3D;&#x3D;&gt; Parameters: 张三abc(String)</span><br><span class="line">DEBUG [main] - &lt;&#x3D;&#x3D;    Updates: 1</span><br><span class="line">DEBUG [main] - &#x3D;&#x3D;&gt;  Preparing: insert into user(username) values(?)</span><br><span class="line">DEBUG [main] - &#x3D;&#x3D;&gt; Parameters: 张三abc(String)</span><br><span class="line">DEBUG [main] - &lt;&#x3D;&#x3D;    Updates: 1</span><br><span class="line">id&#x3D;8</span><br></pre></td></tr></table></figure>



<h1 id="5-使用Mapper接口（实际应用）"><a href="#5-使用Mapper接口（实际应用）" class="headerlink" title="5.使用Mapper接口（实际应用）"></a>5.使用Mapper接口（实际应用）</h1><blockquote>
<p>mybatis建议使用SqlSession.getMapper()方式执行mapper映射语句。<br>这种方式使用起来非常方便。</p>
</blockquote>
<p><strong>1.在mapper包中建立与UserMapper.xml同名、同位置的接口：UserMapper.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> mapper;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> pojo.User;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserMapper</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> User <span class="title">getUserById</span><span class="params">(<span class="keyword">int</span> id)</span></span>;</span><br><span class="line">	<span class="keyword">public</span> List&lt;User&gt;getUserByName(String uname);</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">addUser</span><span class="params">(User user)</span></span>;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">addUserGetId</span><span class="params">(User user)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>前提：</p>
<p>1.接口文件名为UserMapper.java — 与UserMapper.xml同名 </p>
<p>2.两者都在同一个mapper包中</p>
<p>3.UserMapper.xml中命名空间为：maper.UserMapper</p>
<p>4.接口中的每一个方法，对应UserMapper.xml中的id，输入参数和输出参数一致！</p>
</blockquote>
<p><strong>2.应用</strong></p>
<p>以getUserById为例，执行：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">	Reader reader=Resources.getResourceAsReader(<span class="string">&quot;mybatis-config.xml&quot;</span>);</span><br><span class="line">	SqlSessionFactory ssf=<span class="keyword">new</span> SqlSessionFactoryBuilder().build(reader);</span><br><span class="line">	SqlSession sqlSession=ssf.openSession();</span><br><span class="line">	</span><br><span class="line">	UserMapper mapper=sqlSession.getMapper(UserMapper.class);</span><br><span class="line">	User user=mapper.getUserById(<span class="number">8</span>);</span><br><span class="line">	System.out.println(<span class="string">&quot;username=&quot;</span>+user.getUsername());</span><br><span class="line">	sqlSession.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>使用sqlSession.getMapper(接口类.class)方法，获取UserMapper接口的实例mapper。</p>
<p>使用 mapper.接口方法名字(参数);来调用每一个方法。</p>
</blockquote>
<p><strong>3.修改配置文件</strong></p>
<p>以包扫描方式，自动扫面包中所有的mapper接口，实现自动注册。—当然，也可以保留原来的方式不变。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mappers</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!--没使用接口前： &lt;mapper resource=&quot;mapper/UserMapper.xml&quot; /&gt; --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">&quot;mapper&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mappers</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>这样就不需要一个个去注册Mapper.xml文件了。</p>
</blockquote>
<h1 id="6-其他：实现配置文件和映射文件标记自动提示功能"><a href="#6-其他：实现配置文件和映射文件标记自动提示功能" class="headerlink" title="6.其他：实现配置文件和映射文件标记自动提示功能"></a>6.其他：实现配置文件和映射文件标记自动提示功能</h1><p>1.通过链接，打开/下载dtd文件;</p>
<p>2.在eclipse的偏好设置中：windows-&gt;prefrences-&gt;XML–&gt;XML catalog–&gt;add</p>
<p>3.location:File  System导入dtd文件</p>
<p>4.输入key值:-//mybatis.org//DTD Config 3.0//EN        ;   -//mybatis.org//DTD Mapper 3.0//EN</p>
<p>5.key type ：选择：url</p>
</div><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Heldwen</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://gitee.com/drh-space/myblog/03/04/2022/javaEE(SSM)%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B03%EF%BC%9AMybatis%E6%A0%B8%E5%BF%83%E5%AF%B9%E8%B1%A1%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/">https://gitee.com/drh-space/myblog/03/04/2022/javaEE(SSM)%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B03%EF%BC%9AMybatis%E6%A0%B8%E5%BF%83%E5%AF%B9%E8%B1%A1%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://gitee.com/drh-space/myblog" target="_blank">Space</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/myblog/03/11/2022/javaEE(SSM)%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B04%EF%BC%9A%E5%8A%A8%E6%80%81SQL/"><img class="prev-cover" data-lazy-src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="onerror=null;src='/myblog/img/404.jpg'"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">javaEE(SSM)学习笔记4：动态SQL</div></div></a></div><div class="next-post pull-right"><a href="/myblog/03/03/2022/javaEE(SSM)%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B02%EF%BC%9AMybatis%E5%85%A5%E9%97%A8/"><img class="next-cover" data-lazy-src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="onerror=null;src='/myblog/img/404.jpg'"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">javaEE(SSM)学习笔记2：Mybatis入门</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div><div class="comment-switch"><span class="first-comment">Valine</span><label><input id="switch-comments-btn" type="checkbox"/><span class="slider"></span></label><span class="second-comment">Disqus</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div><div><div id="disqus_thread"></div></div></div></div></article></main><footer id="footer" style="background-image: url(https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg)"><div id="footer-wrap"><div class="copyright">&copy;2022 By Heldwen</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">Welcome To My Space !</div></div></footer></div><section id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></section><div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/myblog/js/utils.js"></script><script src="/myblog/js/main.js"></script><script src="/myblog/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module" defer></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.spacingElementById('content-inner')
  else {
    $.getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js', () => {
      pangu.spacingElementById('content-inner')
    })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguFn)</script><div class="js-pjax"><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    $.getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js', function () {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'default',
      })
      false && mermaid.init()
    })
  }
}</script><script>function loadValine () {
  function initValine () {
    const initData = {
      el: '#vcomment',
      appId: '',
      appKey: '',
      placeholder: 'Please leave your footprints',
      avatar: 'monsterid',
      meta: 'nick,mail,link'.split(','),
      pageSize: '10',
      lang: 'zh-CN',
      recordIP: false,
      serverURLs: '',
      emojiCDN: '',
      emojiMaps: "",
      enableQQ: false,
      path: window.location.pathname,
    }

    if (true) { 
      initData.requiredFields= ('nick,mail'.split(','))
    }

    const valine = new Valine(initData)
  }

  if (typeof Valine === 'function') initValine() 
  else $.getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js', initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.querySelector('#vcomment'),loadValine)
  else setTimeout(() => loadValine(), 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script>function loadDisqus () {
  var disqus_config = function () {
    this.page.url = 'https://gitee.com/drh-space/myblog/03/04/2022/javaEE(SSM)%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B03%EF%BC%9AMybatis%E6%A0%B8%E5%BF%83%E5%AF%B9%E8%B1%A1%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/'
    this.page.identifier = '03/04/2022/javaEE(SSM)学习笔记3：Mybatis核心对象及其配置/'
    this.page.title = 'javaEE(SSM)学习笔记3：Mybatis核心对象及其配置'
  };

  window.disqusReset = () => {
    DISQUS.reset({
      reload: true,
      config: disqus_config
    })
  }

  if (window.DISQUS) disqusReset()
  else {
    (function() { 
      var d = document, s = d.createElement('script');
      s.src = 'https://.disqus.com/embed.js';
      s.setAttribute('data-timestamp', +new Date());
      (d.head || d.body).appendChild(s);
    })();
  }
}

if ('Valine' === 'Disqus' || !false) {
  if (false) btf.loadComment(document.getElementById('disqus_thread'), loadDisqus)
  else loadDisqus()
} else {
  function loadOtherComment () {
    loadDisqus()
  }
}
</script><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><canvas class="fireworks" mobile="true"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="true" data-click="true"></script><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = true;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="true"></script></div></body></html>